package controller

import (
	"gitee.com/song-yao-shun/MQH/model"
	"gitee.com/song-yao-shun/MQH/response"
	"gitee.com/song-yao-shun/MQH/respository"
	"gitee.com/song-yao-shun/MQH/vo"
	"github.com/gin-gonic/gin"
	"log"
)

type IPostController interface {
	RestController
}

type PostController struct {
	PostRty respository.PostRepository
}

func (p PostController) Create(ctx *gin.Context) {
	var requestPost vo.CreatePostRequest
	// 数据验证
	if err := ctx.ShouldBind(&requestPost); err != nil {
		log.Println(err.Error())
		response.Fail(ctx, nil, "数据验证失败！")
		return
	}

	// 获取登录用户 user
	user, _ := ctx.Get("user")

	// 创建post
	post := model.Post{
		UserId: 		user.(model.User).ID,
		CategoryId: 	requestPost.CategoryId,
		Title: 			requestPost.Title,
		HeadImg: 		requestPost.HeadImg,
		Content: 		requestPost.Content,
	}

	postId, err := p.PostRty.Create(post)
	if err != nil {
		response.Fail(ctx, nil, "新建失败！")
		return
	}

	// 返回
	response.Success(ctx, gin.H{"data":postId}, "新建成功！")
}

func (p PostController) Update(ctx *gin.Context) {
	var requestPost vo.CreatePostRequest
	// 数据验证
	if err := ctx.ShouldBind(&requestPost); err != nil {
		log.Println(err.Error())
		response.Fail(ctx, nil, "数据验证失败！")
		return
	}

	// 获取 path 中的参数
	postId := ctx.Params.ByName("id")
	updatePost, err := p.PostRty.SelectById(postId)
	if err != nil {
		response.Fail(ctx, nil, "更新的文章不存在！")
		return
	}

	// 判断当前用户是否为文章的作者
	// 获取登录用户 user
	user, _ := ctx.Get("user")
	userId := user.(model.User).ID
	if userId != updatePost.UserId {
		response.Fail(ctx, nil, "文章不属于您，请勿非法操作！")
		return
	}

	// 更新 struct
	updatePost.CategoryId = requestPost.CategoryId
	updatePost.Title = requestPost.Title
	updatePost.HeadImg = requestPost.HeadImg
	updatePost.Content = requestPost.Content

	post, err := p.PostRty.Update(*updatePost)
	if err != nil {
		panic(err)
	}

	response.Success(ctx, gin.H{"post":post}, "修改成功。")
}

func (p PostController) Show(ctx *gin.Context) {
	// 获取 path 中的参数
	postId := ctx.Params.ByName("id")

	post, err := p.PostRty.SelectById(postId)
	if err != nil {
		response.Fail(ctx, nil, "文章不存在！")
		return
	}

	response.Success(ctx, gin.H{"post":post}, "")
}

func (p PostController) Delete(ctx *gin.Context) {
	// 获取 path 中的参数
	postId := ctx.Params.ByName("id")

	post, err := p.PostRty.SelectById(postId)
	if err != nil {
		response.Fail(ctx, nil, "文章不存在！")
		return
	}

	// 判断当前用户是否为文章的作者
	// 获取登录用户 user
	user, _ := ctx.Get("user")
	userId := user.(model.User).ID
	if userId != post.UserId {
		response.Fail(ctx, nil, "文章不属于您，请勿非法操作！")
		return
	}

	// 执行删除
	if rowsAffected := p.PostRty.DeleteById(postId);rowsAffected != 1 {
		response.Fail(ctx, nil, "删除失败，请重试！")
		return
	}

	response.Success(ctx, nil, "删除成功！")
}

func (p PostController) Select(ctx *gin.Context) {
	panic("代码待补充！")
}

func (p PostController) List(ctx *gin.Context) {
	// 接收参数
	var requestPostList vo.PostList

	if err := ctx.ShouldBind(&requestPostList); err != nil {
		response.Fail(ctx, nil, "数据验证错误，pageNum 或者 pageSize 的数据类型验证错误！")
		return
	}else{
		if requestPostList.PageNum == 0{
			requestPostList.PageNum = 1
		}
		if requestPostList.PageSize == 0{
			requestPostList.PageSize = 20
		}
	}
	//// 也可以通过这种方式获取入参
	//PageNum, _ := strconv.Atoi(ctx.DefaultQuery("PageNum", "1"))

	posts,total := p.PostRty.List(requestPostList.PageNum, requestPostList.PageSize)

	response.Success(ctx, gin.H{"data":posts, "total":total}, "查询列表成功。")
}

func NewPostController() IPostController {
	return PostController{PostRty: respository.NewPostRepository()}
}